﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HierarchicalClustersGenerator
{
    class Program
    {
        static void Main(string[] args)
        {

            HierarchialClustersGenerator hgc = new HierarchialClustersGenerator();
//            Dictionary<string, Document> documents = ReadDocs("smallTfIdf500.txt");
//            Dictionary<string, Document> documents = ReadDocs("smallTfIdf1000.txt");
            Dictionary<string, Document> documents = ReadDocs("tf_idf_500.txt");
//            Dictionary<string, Document> documents = ReadDocs("tf_idf_1000.txt");
//            Dictionary<string, Document> documents = ReadDocs("tfidfMed500.txt"); tfIdfMed1000
//            Dictionary<string, Document> documents = ReadDocs("tfIdfMed1000.txt"); 


            hgc.MinDocsPerCluster = 100;
            Dictionary<string, List<string>> clusters = hgc.GenerateHierarchialClusters(documents);

            printClustersToFile(clusters);

            Console.Write("end");

        }


        public static Dictionary<string, Document> ReadDocs(string fileName)
        {
            Dictionary<string, Document> toRetrurn = new Dictionary<string, Document>();
            string line,docName;
            List<double> tfIdf = new List<double>();
            string[] lineArr;
            string weight = "";
            int counter = 1;
            Document doc = new Document("");

            System.IO.StreamReader file = new System.IO.StreamReader(fileName);
            while ((line = file.ReadLine()) != null)
            {
                if (counter % 2 == 1) // if the current line is the document's name
                {
                    lineArr = line.Split('#');
                    docName = lineArr[0];
                    doc = new Document(docName);
                }
                else  // the current line is the doc's tf-idf weights
                {
                    //if (counter == 84)
                    //    counter = 84;
                    lineArr = line.Split(',');
                    doc.tfIdfList = new List<double>();
                    for (int i = 0; i < lineArr.Length; i++)
                    {
                        if (i == lineArr.Length - 1) // special technical case
                        {
                            weight = lineArr[i];
                            weight = weight.Substring(0, weight.Length - 2);
                            doc.tfIdfList.Add(Convert.ToDouble(weight));
                        }
                        else
                            doc.tfIdfList.Add(Convert.ToDouble(lineArr[i]));
                    }
                    toRetrurn.Add(doc.Name, doc);
                }

                counter++;
            }

            file.Close();

            return toRetrurn;
        }

        /// <summary>
        /// writing the herarchial clusters results into file. every line in the format:
        /// cluster_name,num_of_docs,doc2,doc6,doc17......
        /// </summary>
        /// <param name="clusters"></param>
        public static void printClustersToFile(Dictionary<string, List<string>> clusters)
        {
            using (System.IO.StreamWriter file = new System.IO.StreamWriter("her_clusters.txt"))
            {
                foreach (KeyValuePair<string, List<string>> cluster in clusters)
                {
                    file.Write(cluster.Key + "," + cluster.Value.Count + ",");
                    foreach (string str in cluster.Value)
                    {
                        file.Write(str + ",");
                    }
                    file.WriteLine();
                }
                file.Close();
            }
        }

        public static List<int> CreateSampleFeatures()
        {
            List<int> toReturn = new List<int>();
            toReturn.Add(10000);
            toReturn.Add(11111);
            toReturn.Add(22222);
            toReturn.Add(33333);
            toReturn.Add(44444);
            toReturn.Add(55555);

            return toReturn;
        }


        public static Dictionary<string, List<double>> CreateSampleDocuments()
        {
            Dictionary<string, List<double>> toReturn = new Dictionary<string, List<double>>();
            List<double> weights = new List<double>();

            weights.Add(0);
            weights.Add(0);
            weights.Add(0);
            weights.Add(0);
            weights.Add(0);
            weights.Add(0.08);
            toReturn.Add("doc1", new List<double>(weights));
            weights.Clear();

            weights.Add(0);
            weights.Add(0);
            weights.Add(0);
            weights.Add(0);
            weights.Add(0);
            weights.Add(0.001);
            toReturn.Add("doc2", new List<double>(weights));
            weights.Clear();

            weights.Add(0);
            weights.Add(0.002);
            weights.Add(0.001);
            weights.Add(0);
            weights.Add(0.00002);
            weights.Add(0.00003);
            toReturn.Add("doc3", new List<double>(weights));
            weights.Clear();

            weights.Add(0);
            weights.Add(0.09);
            weights.Add(0.003);
            weights.Add(0.0);
            weights.Add(0.0);
            weights.Add(0.0);
            toReturn.Add("doc4", new List<double>(weights));
            weights.Clear();

            return toReturn;

        }
    }
}
